package com.luxondata.module.utils;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class GenerateBaseTemplate {
	static String  classbase ="F:\\000Luxondata\\cloudserver\\";
	static String generateNameUp ="Distributors";
	static String generateNameLow ="dists";
	static String generateNamecn ="经销商";
	static String basepackage="masterdatas";

	/**
	 * @param args
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, SQLException  {
		//Connection conn =null;
		//Statement stmt = null; 
		//Class.forName("com.mysql.jdbc.Driver");
		//Class.forName("oracle.jdbc.driver.OracleDriver"); 
		//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		//conn = DriverManager.getConnection("jdbc:mysql://ykc:3306/luxondata", "root","123456"); 
		//conn = DriverManager.getConnection("jdbc:oracle:thin:@ykc:1521:orcl", "luxondata","luxondata"); 
		//conn = DriverManager.getConnection("jdbc:sqlserver://114.141.160.99:1433; DatabaseName=luxondata", "sa","Aspial1$"); 
		//stmt = conn.createStatement(); 
		//String sql = "CREATE TABLE T_BD_"+generateNameUp+"( fid  INT NOT NULL, fnumber   VARCHAR(255), fname varchar(255) )"; 
		//stmt.executeUpdate(sql); 
		
		//String sqlseq= "insert into SEQUENCE(NAME,CURRENT_VALUE,INCREMENT) VALUES('T_"+generateNameUp+"SEQ',100000000,1)";
		
		//String sqlseq ="CREATE SEQUENCE T_BD_"+generateNameUp+"SEQ MINVALUE 1 MAXVALUE 99999999 INCREMENT BY 1 START WITH 10000001 CACHE 20 NOORDER CYCLE ";
		//stmt.executeUpdate(sqlseq); 

		
		//controller  com.luxondata.module.system.controller
		String objout1=classbase+"src\\main\\java\\com\\luxondata\\module\\"+basepackage+"\\controller\\"+generateNameUp+"Controller.java";
		String objin1=classbase+"src\\main\\java\\com\\luxondata\\module\\masterdata\\controller\\ProductController.java";
		writeFile(objout1, objin1);

		
		// entity com.luxondata.module.system.entry
		objout1=classbase+"src\\main\\java\\com\\luxondata\\module\\"+basepackage+"\\entry\\"+generateNameUp+".java";
		objin1=classbase+"src\\main\\java\\com\\luxondata\\module\\masterdata\\entry\\Product.java";
		writeFile(objout1, objin1);

		
		// repository com.luxondata.module.system.repository
		objout1=classbase+"src\\main\\java\\com\\luxondata\\module\\"+basepackage+"\\repository\\"+generateNameUp+"Repository.java";
		objin1=classbase+"src\\main\\java\\com\\luxondata\\module\\masterdata\\repository\\ProductRepository.java";
		writeFile(objout1, objin1);
		
		// services com.luxondata.module.system.service
		objout1=classbase+"src\\main\\java\\com\\luxondata\\module\\"+basepackage+"\\service\\"+generateNameUp+"Service.java";
		objin1=classbase+"src\\main\\java\\com\\luxondata\\module\\masterdata\\service\\ProductService.java";
		writeFile(objout1, objin1);

	
	}

	private static void writeFile(String objout1, String objin1) {
		File   f = new File(objout1.substring(0,objout1.lastIndexOf(File.separator)));
		f.mkdirs();
		
		BufferedReader reader = null; 
		BufferedWriter writer = null;
		try {	
			reader = new BufferedReader(new FileReader(objin1));
			writer = new BufferedWriter(new FileWriter(objout1));
			String data = reader.readLine();//一次读入一行，直到读入null为文件结束 
			while( data!=null){ 
			 writer.write(data.replace("product", generateNameLow).replace("Product", generateNameUp).replace("PRODUCT", generateNameUp).replace("产品", generateNamecn)
					 .replace("masterdata", basepackage)); 
			 writer.write("\n");
			 data = reader.readLine(); //接着读下一行 
			}
			 writer.flush(); 
			 reader.close(); 
			 writer.close(); 
		} catch (IOException e) {
			e.printStackTrace();
		}  finally {
		
		}
	}
}
